<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keywords" placeholder="店铺名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
          <el-button type="primary" @click="dialogVisible = true">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table border :data="tableData" highlight-current-row v-loading="listLoading"
              style="width: 100%;">
      <el-table-column prop="title" label="标题">
      </el-table-column>
      <el-table-column prop="content" label="内容">
        <template slot-scope="scope">
          <span v-html="scope.row.content"></span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="发布时间">
      </el-table-column>
      <el-table-column prop="createUser" label="发布人">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>
    <el-dialog title="新增/编辑公告" :visible.sync="dialogVisible" @close="dialogClose" width="30%">
      <el-form :model="announcement" label-width="80px" ref="rules">
        <el-form-item label="标题">
          <el-input v-model="announcement.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <quill-editor v-model="announcement.content"
                        :options="editorOption">
          </quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="dialogClose">取消</el-button>
          <el-button type="success" @click="saveOrUpdate()">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </section>
</template>

<script>
import {quillEditor} from "vue-quill-editor"; // 在页面中引入富文本编辑框的组件
import 'quill/dist/quill.core.css';   // 引入样式
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import quillConfig from "../../common/js/quill-config";
export default {
  components: {
    quillEditor   // 如果key和value的名字一样,可以省略 quillEditor:quillEditor
  },
  name: "Announcement",
  data() {
    return {
      editorOption: quillConfig,
      remark: '',
      query: {
        keywords: ''
      },
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: [],
      fileList: [],
      listLoading: false,
      selection: [],
      dialogVisible: false,
      uploadDialog: false,
      title: '',
      announcement: {},
    }
  },
  mounted() {
    this.getAnnouncementList();
  },
  methods: {
    search() {
      this.currentPage = 1;
      this.getAnnouncementList();
    },
    getAnnouncementList() {
      this.$axios.post('/announcement', {
        keywords: this.query.keywords,
        page: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        if (res.data.code === 1) {
          this.tableData = res.data.data.rows;
          this.total = res.data.data.total;
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAnnouncementList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getAnnouncementList();
    },
    dialogClose() {
      this.announcement = {};
      this.dialogVisible = false;
    },
    handleEdit(row) {
      this.dialogVisible = true;
      this.announcement = {...row};
    },
    saveOrUpdate() {
      this.$axios.put('/announcement', this.announcement).then(res => {
        if (res.data.code === 1) {
          this.$message.success("操作成功");
          this.dialogClose();
          this.getAnnouncementList();
        }
      })
    },
    handleDel(id) {
      this.$confirm('确认删除该公告吗？', '提示', {type: 'warning'}).then(() => {
        this.listLoading = true;
        this.$axios.delete('/announcement/' + id).then((res) => {
          this.listLoading = false;
          if (res.data.code === 1) {
            this.$message.success("删除成功")
            //如果当前页数据被删除完了，则自动切换到上一页
            if (this.tableData.length === 1 && this.currentPage > 1) {
              this.currentPage--;
            }
            this.getAnnouncementList();
          } else {
            this.$message.error(res.data.msg || '删除失败');
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>